package com.yoga.manager.app.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.SendSmsBO;
import com.yoga.common.bo.SmsSendResponse;
import com.yoga.common.enumcommon.SmsBusinessEnum;
import com.yoga.manager.app.SmsManager;
import com.yoga.manager.smsUtil.SmsSendUtil;
import com.yoga.manager.uitl.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by chenjin on 2020/4/13.
 */
@Component
public class SmsManagerImpl implements SmsManager {


    private Logger log = LoggerFactory.getLogger(SmsManagerImpl.class);

    @Override
    public Boolean sendSms(String phone, SmsBusinessEnum smsBusinessEnum, SendSmsBO sendSmsBO) throws AppException {
        if (smsBusinessEnum==null){
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(phone)){
            log.info("SmsSendUtil.sendSms phone is null  ,smsBusinessEnum:{},sendSmsBO:{}",phone,smsBusinessEnum.name(), JsonUtil.toJson(sendSmsBO));
            throw new AppException(AppErrorCodeEnum.PHONE_IS_NULL);
        }

        log.info("SmsSendUtil.sendSms phone:{} ,smsBusinessEnum:{},sendSmsBO:{}",phone,smsBusinessEnum.name(), JsonUtil.toJson(sendSmsBO));
        SmsSendResponse smsSendResponse= new SmsSendUtil().sendSms(phone,smsBusinessEnum,sendSmsBO);

        if (smsSendResponse==null){
            log.error("参数为空 SmsSendUtil.sendSms phone:{} ,smsBusinessEnum:{},sendSmsBO:{}",phone,smsBusinessEnum.name(), JsonUtil.toJson(sendSmsBO));
        }
        if ("0".equals(smsSendResponse.getCode())){
            return Boolean.TRUE;
        }
        log.error("SmsManagerImpl.smsSendResponse{}", JsonUtil.toJson(smsSendResponse));
        throw new AppException(AppErrorCodeEnum.SYSTEM_ERROR);
    }







}
